<?php
/**
 * CGridManager class file.
 *
 * @author Lucas Vasconcelos <contato@lucasvasconcelos.com>
 * @copyright Copyright(c) 2010 Lucas Vasconcelos
 * @license BSD License http://www.vasconcelossa.com.br/yap/license
 * @link http://www.vasconcelossa.com.br/yap/
 * @version $Id: CGridManager.php 15 2010-02-03 01:08:53Z contato@lucasvasconcelos.com $
 * @package yap.web.ui
 */

Prado::using('yap.web.ui.interfaces');

/**
 * CGridManager class.
 * 
 * Encampsula rotinas de manipulação do objeto TDataGrid e a sessão do objeto TActiveRecordCriteria.
 * 
 * @author Lucas Vasconcelos <contato@lucasvasconcelos.com>
 * @version 1.0 $Revision: 15 $
 * @package yap.web.ui
 */
class CGridManager
{
	/**
	 * @var IPageGrid
	 */
	private $_parent=null;
	
	public function getParent()
	{
		return $this->_parent;
	}
	
	/**
	 * Construtor.
	 * Registra objeto IPageGrid pai.
	 * @param IPageGrid
	 */
	public function __construct(IPageGrid $parent)
	{
		$this->_parent=$parent;
	}

	/**
	 * Handler do evento OnPagerCreated.
	 * 
	 * Adiciona um label ao componente de paginação.
	 */
	public function pagerCreated($sender,$param)
	{
		$param->getPager()->getControls()->insertAt(0, 'Página: ');
	}

	/**
	 * Handler do evento OnPageIndexChanged.
	 */
	public function pageIndexChanged($sender,$param)
	{
		$this->_parent->{IPageGrid::GRID_ID}->setCurrentPageIndex($param->getNewPageIndex());
		
		Prado::trace('GridManager onLoadGrid()','yap.web.ui');
		$this->_parent->onLoadGrid(null);
	}

	/**
	 * Handler do evento OnSortCommand.
	 */
	public function sortCommand($sender,$param)
	{
		$c=$this->getCriteria();

		if(isset($c->OrdersBy[$param->SortExpression]))
			$dir=$c->OrdersBy[$param->SortExpression];
		$dir=(isset($dir) && $dir==='asc') ? 'desc' : 'asc';

		// if singleSort was defined, clear previous sort data.
		if(TPropertyValue::ensureBoolean(Prado::getApplication()->Parameters['OrderBySingle']))
		{
			$c->OrdersBy->clear();
			foreach($sender->getColumns() as $column)
				$column->HeaderStyle->setCssClass('');
		}

		// set css class to sorted column.
		foreach($sender->getColumns() as $column)
		{
			if($column->getSortExpression()==$param->getSortExpression())
				$column->HeaderStyle->CssClass="order_{$dir}";
		}
		$c->OrdersBy[$param->SortExpression]=$dir;

		$this->setCriteria($c);

		Prado::trace('GridManager onLoadGrid()', 'yap.web.ui');
		$this->_parent->onLoadGrid(null);
	}

	/**
	 * Set of Criteria property.
	 * @param TActiveRecordCriteria
	 */
	public function setCriteria(TActiveRecordCriteria $criteria)
	{
		Prado::getApplication()->getSession()->add($this->getCriteriaId(), base64_encode(serialize($criteria)));
	}

	/**
	 * Get of Criteria property.
	 * @return TActiveRecordCriteria
	 */
	public function getCriteria()
	{
		if(!Prado::getApplication()->getSession()->contains($this->getCriteriaId()))
			return new TActiveRecordCriteria;
		return unserialize(base64_decode(Prado::getApplication()->getSession()->itemAt($this->getCriteriaId())));
	}

	/**
	 * Clear criteria from session.
	 */
	public function clearCriteria()
	{
		Prado::getApplication()->getSession()->remove($this->getCriteriaId());
	}

	/**
	 * @return string id of the session that stores AR criteria object.
	 */
	protected function getCriteriaId()
	{
		return 'crt'.$this->_parent->getName();
	}
}
